﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Connect_to_SQL_Server_using_ADO.NET
{
    public partial class Form1 : Form
    {
        BindingSource tblNamesBS = new BindingSource();
        DataSet ds = new DataSet();
        IContactsRepository contactsRepository;

        public Form1(IContactsRepository contactsRepository)
        {
            InitializeComponent();
            this.contactsRepository = contactsRepository;
            contactsRepository.Read(ds);
            dg.DataSource = ds.Tables[0];
            tblNamesBS.DataSource = ds.Tables[0];
            txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FIRSTNAME"));
            txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LASTNAME"));
            btnAdd.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            contactsRepository.Add(txtFirstName.Text, txtLastName.Text);
            txtFirstName.Text = "";
            txtLastName.Text = "";

            display();
            btnAdd.Enabled = false;
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            display();
            
            tblNamesBS.DataSource = ds.Tables[0];
            txtFirstName.DataBindings.Clear();
            txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FIRSTNAME"));
            txtLastName.DataBindings.Clear();
            txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LASTNAME"));
            StatusLabel1.Text = "Records Display";
            btnAdd.Enabled = false;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            tblNamesBS.MoveNext();
            dgUpdate();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            tblNamesBS.MoveLast();
            dgUpdate();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            tblNamesBS.MovePrevious();
            dgUpdate();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            tblNamesBS.MoveFirst();
            dgUpdate();
        }

        private void dgUpdate()
        {
            dg.ClearSelection();
            dg.Rows[tblNamesBS.Position].Selected = true;
        }
        
        
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //TODO: if Firstname and Lastname havent changed then no Update need to been done. MsgBox for user.
            // if there isn t any record handle expection
            try
            {
                contactsRepository.Update(Convert.ToInt32(ds.Tables[0].Rows[tblNamesBS.Position][0]), txtFirstName.Text, txtLastName.Text);
                StatusLabel1.Text = "Record has been updated";
                dgUpdate();
            }
            catch (Exception)
            {
                throw new FormatException();
            }
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            DialogResult dr;
            dr = MessageBox.Show("Are you sure you want to delete the record?", "Delete Confirmation",MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                contactsRepository.Delete(txtFirstName.Text,txtLastName.Text);
                display();
            }
            StatusLabel1.Text = "Record(s) has been deleted";
            
            
        }

        private void display()
        {
            contactsRepository.Read(ds);
            dg.DataSource = ds.Tables[0];
        }

        private void buttonPlus_Click(object sender, EventArgs e)
        {
            btnAdd.Enabled = true;
            txtFirstName.Text = "";
            txtLastName.Text = "";
            StatusLabel1.Text = "Fill in the blanks and press Add";
            
        }
    }
}
